import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path'

// 路径拼接
function resolve (dir: string) {
  return path.join(__dirname, dir)
}

export default ({ mode }) =>
  defineConfig({
    plugins: [vue()],
    resolve: {
      alias: {
        '@': resolve('src')
      }
    },
    // 代理服务器，解决跨域请求问题
    server: {
      proxy: {
        [loadEnv(mode, process.cwd()).VITE_BASE_API]: {
          target: loadEnv(mode, process.cwd()).VITE_BACKEND_URL,
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/api/, '')
        }
      }
    },
    // css配置
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@import "@/assets/style/variables.scss";'
        }
      }
    }
  })
